#include <iostream>
#include "Array.h"

using namespace std;

Array::Array(size_t size)
{
    this->_capacity = size;
    this->_size = size;
    this->data = new int[this->_capacity];
}

Array::Array(const Array& arr)
{
    this->_capacity = arr._capacity;
    this->data = new int[this->_capacity];
    this->_size = arr._size;
    for(size_t i = 0; i < this->_size;i++){
        this->data[i] = arr.data[i];
    }
}

Array::~Array()
{
    delete[] this->data;
}

Array& Array::operator=(const Array& arr)
{
    if(this != &arr) {
        delete [] this->data;
        this->_size = arr._size;
        this->data = new int[this->_size];
        for (size_t i = 0; i <this-> _size; i++){
            this->data[i] = arr.data[i];
        }
    }
    return *this;
}

void Array::pushBack(int value)
{
    if(this->_size >= this->_capacity){
        this->encreaseCapacity();
    }
    this->set(this->_size++, value);
}

int Array::get(size_t index)
{
    this->check(index);
    return this->data[index];
}

void Array::set(size_t index, int value)
{
    this->check(index);
    this->data[index] = value;
}

void Array::check(size_t index)
{
    if(index >= this -> _size || index < 0){
        throw "Index out of bounds";
    }    
} 

size_t Array::size()
{
    return this->_size;
}

void Array::encreaseCapacity()
{
    size_t newCapacity = this->_capacity * 2;
    int *newData = new int[newCapacity];
    for(size_t i = 0; i < this->_size; i++){
        newData[i] = this->data[i];
    }
    delete[] this->data;
    this->data = newData;
    this->_capacity = newCapacity;
}

int main()
{
    Array a(5);
    a.set(0,5);
    cout << a.get(0) << "\n";
    a.pushBack(11);
    cout << a.get(5) << "\n";    
    return 0;
}